#ANSWERS
RevenueInfo = '''Q1-2018,Exercise_Fitness,10.33;
Q1-2018,Outdoor_Play_Equipment,7.85;
Q1-2018,Winter_Sports,3.45;
Q2-2018,Exercise_Fitness,7.63;
Q2-2018,Outdoor_Play_Equipment,5.05;
Q2-2018,Winter_Sports,-;
Q3-2018,Exercise_Fitness,1.31;
Q3-2018,Outdoor_Play_Equipment,3.95;
Q3-2018,Winter_Sports,1.50;
Q4-2018,Exercise_Fitness,5.71;
Q4-2018,Outdoor_Play_Equipment,6.52;
Q4-2018,Winter_Sports,4.15'''
# ANSWERS
def validateRecord(recStr):
# Split the string by comma
fields = recStr.split(",")
# Wrap the code around try/except to handle exceptions
try:
# If the number of fields in the string is less than 3, then throw an exception
if len(fields) < 3:
raise Exception("Expected 3 fields. Found only ", len(fields))
# Convert the third field (revenue) into float type
revenue = float(fields[2])
# If we have reached here without any problem, then it is a good rec. Tag the record GOOD and return
return ("GOOD", recStr)
# If we have reached here then it is a bad rec. Tag the record BAD and return
except Exception as ex:
return ("BAD", recStr)
# TEST - Run this cell to test your solution.
test1Str = validateRecord("Q1-2018,Exercise_Fitness,10.13")
test1StrExpected = ("GOOD","Q1-2018,Exercise_Fitness,10.13")
test2Str = validateRecord("Q1-2018,Exercise_Fitness,as")
test2StrExpected = ("BAD","Q1-2018,Exercise_Fitness,as")
test3Str = validateRecord("Q1-2018,Exercise_Fitness")
test3StrExpected = ("BAD","Q1-2018,Exercise_Fitness")
assert test1Str == test1StrExpected, "Expected the total to be " + str(test1StrExpected) + " but found " + str(test1Str)
assert test2Str == test2StrExpected, "Expected the total to be " + str(test2StrExpected) + " but found " + str(test2Str)
assert test3Str == test3StrExpected, "Expected the total to be " + str(test3StrExpected) + " but found " + str(test3Str)
# ANSWERS
def parsetData(dataStr):
# split the string by ';' to get individual record strings
recList = dataStr.split(";")
# validate each record
allRecs = list(map(lambda rec: validateRecord(rec.strip()), recList))
# based on GOOD and BAD tag, store in goodRecs and badRecs variable
badRecs = list(filter(lambda rec: rec[0] == "BAD", allRecs))
goodRecs = list(filter(lambda rec: rec[0] == "GOOD", allRecs))
# return the tuple
return (goodRecs, badRecs)
# TEST - Run this cell to test your solution.
goodrecsLen = len(goodrecs)
goodrecsLenExpected = 11
badrecsLen = len(badrecs)
badrecsLenExpected = 1
assert goodrecsLen == goodrecsLenExpected, "Expected the total to be " + str(goodrecsLenExpected) + " but found " + str(goodrecsLen)
assert badrecsLen == badrecsLenExpected, "Expected the total to be " + str(badrecsLenExpected) + " but found " + str(badrecsLen)
# TEST - Run this cell to test your solution.
caseClass = convertToClass("Q1-2018,Exercise_Fitness,10.33")
caseClassExpected = CategoryQuarterlyRecord("Q1-2018","Exercise_Fitness",10.33)
assert caseClass == caseClassExpected, "Expected the total to be " + str(caseClassExpected) + " but found " + str(caseClass)
# ANSWERS
from functools import reduce
class CompanyPerformance:
# class level variable to store the good records
CategoryRecords = None
#defining constructor
def __init__(self, categoryRecs):
self.CategoryRecords = categoryRecs
# calculates and returns the total revenue across all quarters and categories
def getTotalRevenue(self):
# Extract revenue column and sum up
allRevenues = list(map(lambda rec: rec.revenue, self.CategoryRecords))
totalRevenue = reduce(lambda x, y: x + y, allRevenues)
return round(totalRevenue, 2)
# calculates and returns total revenue by the cateogry (passed as argument to the function)
def getCategoryRevenue(self, _category):
# filter records by category, extract revenue column and sum up
categoryRecs = filter(lambda rec: rec.category == _category, self.CategoryRecords)
allRevenues = list(map(lambda rec: rec.revenue, categoryRecs))
totalRevenue = reduce(lambda x, y: x + y, allRevenues)
return round(totalRevenue, 2)
# TEST - Run this cell to test your solution.
totalRevenueExpected = 57.45
totalCategoryRevenueExpected = 24.98
assert totalRevenue == totalRevenueExpected, "Expected the total to be " + str(totalRevenueExpected) + " but found " + str(totalRevenue)
assert totalRevenueInCategory == totalCategoryRevenueExpected, "Expected the total to be " + str(totalCategoryRevenueExpected) + " but found " + str(totalRevenueInCategory)
Last refresh: Never